package com.gbs.info2;

import com.gbs.info2.tools.ConfigFileHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class Settings extends Activity implements View.OnClickListener {
	private Button saveSettBtn, resetBtn;
	private TextView urlField;
	private CheckBox autoUpdateCheck;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		ConfigFileHelper configFileHelper = new ConfigFileHelper(this);
		saveSettBtn = (Button) findViewById(R.id.saveSettBtn);
		saveSettBtn.setOnClickListener(this);
		resetBtn = (Button)findViewById(R.id.resetBtn);
		resetBtn.setOnClickListener(this);
		urlField = (TextView) findViewById(R.id.urlField);
		urlField.setText(configFileHelper.readLine(2));
		autoUpdateCheck = (CheckBox)findViewById(R.id.autoUpdateCheck);
		autoUpdateCheck.setChecked(false);
		if (configFileHelper.readLine(3).equals("1")){
			autoUpdateCheck.setChecked(true);
		}
	}
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		boolean change = false;
		if (saveSettBtn.getId()==((Button)arg0).getId()){
			ConfigFileHelper configFileHelper = new ConfigFileHelper(this);
			if(!configFileHelper.readLine(2).equals(urlField.getText().toString())){
				configFileHelper.writeLine(2, urlField.getText().toString());
				Toast.makeText(this,getString(R.string.urlChangedOK) , 2000).show();
				change = true;
				Intent intent = new Intent (this, VehicleList.class);
				startActivity(intent);
				this.finish();
			}
			if (stringToBooelan(configFileHelper.readLine(3)) !=(autoUpdateCheck.isChecked())){
				configFileHelper.writeLine(3, booleanToString(autoUpdateCheck.isChecked()));
				Toast.makeText(this,getString(R.string.autoUpdateChangedOK) , 2000).show();
				change = true;
				Intent intent = new Intent (this, VehicleList.class);
				startActivity(intent);
				this.finish();
			}
			if (!change) {
				Toast.makeText(this,getString(R.string.noChange) , 2000).show();
			}
		}else if (resetBtn.getId()==((Button)arg0).getId()) {
			ConfigFileHelper configFileHelper = new ConfigFileHelper(this);
			if (configFileHelper.deleteConfigFile()){
				Toast.makeText(this,getString(R.string.resetConfig) , 2000).show();
				Intent intent = new Intent (this, GBSinfo2Activity.class);
				startActivity(intent);
				this.finish();
			}else {
				Toast.makeText(this,getString(R.string.resetConfigFailed) , 2000).show();
			}
		}
	}
	private boolean stringToBooelan (String value){
		if (value.equals("1")){
			return true;
		}else {
			return false;
		}
	}
	private String booleanToString (Boolean value){
		if (value == true){
			return "1";
		}else {
			return "0";
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			moveTaskToBack(true);
			finish();
			startActivity(new Intent(this, VehicleList.class));
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
}
